package aom.ju.ss.view.dialog

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Build
import android.view.*
import androidx.databinding.DataBindingUtil
import aom.ju.ss.R
import aom.ju.ss.databinding.DialogListLayoutBinding
import aom.ju.ss.lpInterface.TitleDialogInterface

class TitleDialog {
    private lateinit var dialog: Dialog
    private lateinit var binding: DialogListLayoutBinding
    fun show(passWordDetails: Activity?, vvc: TitleDialogInterface?): TitleDialog {
        dialog = Dialog(passWordDetails!!, R.style.dialog)

        binding = DataBindingUtil.inflate(
            LayoutInflater.from(passWordDetails),
            R.layout.dialog_list_layout,
            null,
            false
        )
        dialog.setContentView(binding.rootView)
        val window = dialog.window
        window?.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
            val mContentView = window.findViewById<ViewGroup>(Window.ID_ANDROID_CONTENT)
            val mChildView = mContentView.getChildAt(0)
            if (mChildView != null) {
                mChildView.fitsSystemWindows = false
                mChildView.requestApplyInsets()
            }
        }
        val lp = dialog.window?.attributes
        lp?.dimAmount = 0.3f
        binding.tvDetermine.setOnClickListener { v: View? ->
            vvc?.ok(v, true)
            dialog.dismiss()
        }
        binding.tvCancel.setOnClickListener { v: View? ->
            vvc?.ok(v, false)
            dialog.dismiss()
        }


        dialog.show()
        return this
    }

    fun dialogDismiss() {
        dialog.dismiss()
    }


    fun setConnect(Connect: String?): TitleDialog {
        binding.tvConnect.text = Connect
        return this
    }

    fun gosntvCancel(): TitleDialog {
        binding.tvDetermine.setBackgroundResource(R.drawable.bg_click_dialog_left_right_gray)
        binding.tvCancel.visibility = View.GONE
        binding.line.visibility = View.GONE
        return this
    }

    fun gosntvDetermine(): TitleDialog {
        binding.line.visibility = View.GONE
        binding.tvDetermine.visibility = View.GONE
        binding.tvCancel.setBackgroundResource(R.drawable.bg_click_dialog_left_right_gray)
        return this
    }

    fun tvDetermine(Connect: String?): TitleDialog {
        binding.tvDetermine.text = Connect
        return this
    }

    fun tvCancel(Connect: String?): TitleDialog {
        binding.tvCancel.text = Connect
        return this
    }

    fun setCanceledOnTouchOutside() {
        dialog.setCanceledOnTouchOutside(true)
    }

    companion object {
        @JvmStatic
        fun showMsg(activity: Activity?, msg: String?) {
            TitleDialog()
                .show(activity, null)
                .setConnect(msg)
                .gosntvCancel()
        }

        @JvmStatic
        fun showMsg(activity: Activity?, msg: String?,c :TitleDialogInterface) {
            TitleDialog()
                .show(activity, c)
                .setConnect(msg)
                .gosntvCancel()
        }
    }
}